<?php

namespace app\admin\controller;

use think\Db;
use think\Exception;
use think\response\Json;

Class Order extends Base
{

    public function refund_log(){
        $param = $this->request->get();
        $obj = Db::table("tb_order_refund_log")->order('id', 'desc');

        if (isset($param['card']) && $param['card']) {
            $obj->where('card',$param['card']);
        }
        if (isset($param['order_id']) && $param['order_id']) {
            $obj->where('order_id',$param['order_id']);
        }
        if (isset($param['start']) && $param['start']) {
            $obj->where('refund_time', '>', strtotime($param['start'] . " 00:00:00"));
        }
        if (isset($param['end']) && $param['end']) {
            $obj->where('refund_time', '<', strtotime($param['end'] . " 00:00:00"));
        }
        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    public function withdrawal_record(){
        $param = $this->request->get();
        $obj = Db::table("tb_shop_withdrawal_record")->field("record.*,shop.nickname as shop")->alias("record")->order('id', 'desc');

        if (isset($param['shop_id']) && $param['shop_id']) {
            $obj->where('record.uid',$param['shop_id']);
        }
        if (isset($param['status']) && $param['status']&&$param['status']>-1) {
            $obj->where('record.status',$param['status']);
        }
        if (isset($param['deal_status']) && $param['deal_status']&&$param['deal_status']>-1) {
            $obj->where('record.deal_status',$param['deal_status']);
        }
        if (isset($param['start']) && $param['start']) {
            $obj->where('record.apply_time', '>', strtotime($param['start'] . " 00:00:00"));
        }
        if (isset($param['end']) && $param['end']) {
            $obj->where('record.apply_time', '<', strtotime($param['end'] . " 00:00:00"));
        }
        $obj->join('tb_card_shop shop',"record.uid=shop.id","left");
        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);

        $shops =  Db::table("tb_card_shop")->select();
        return view('', ['list' => $list,'shops'=>$shops]);
    }

    public function withdrawal_detail(){
        $id = $this->request->param('id');

        $obj = Db::table("tb_shop_withdrawal_record")->field("record.*,shop.nickname as shop")->alias("record")->order('record.status asc, record.apply_time desc');
        $obj->join('tb_card_shop shop',"record.uid=shop.id","left");
        $row = $obj->where("record.id",$id)->find();
        $row["to_num"] = $row["money"]-$row["fee_money"];
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data["sp_time"]=time();
            Db::startTrans();
            try{
                if ($data["status"]==1){
                    $res =Db::table("tb_shop_withdrawal_record")->where("id",$id)->update($data);
                    if (!$res){
                        Db::rollback();
                        return ['status' => 'n', 'info' => '操作失败'];
                    }
                }else if ($data["status"]==2){;

                    $updateRes = Db::table("tb_card_shop")->where("id",$row["uid"])->setInc("money",$row["money"]);
                    if (!$updateRes){
                        Db::rollback();
                        return ['status' => 'n', 'info' => '返还金额失败'];
                    }

                    $res =Db::table("tb_shop_withdrawal_record")->where("id",$id)->update($data);
                    if (!$res){
                        Db::rollback();
                        return ['status' => 'n', 'info' => '操作失败'];
                    }
                }else{
                    Db::rollback();
                    return ['status' => 'n', 'info' => '审核状态不对'];
                }
                Db::commit();
                return ['status' => 'y', 'info' => '操作成功'];
            }catch (Exception $e){
                return ['status' => 'n', 'info' => '异常：'.$e->getMessage()];
            }

        }
        return view('', ['data' => $row]);
    }

    public function bouns_record(){
        $obj = Db::table("tb_shop_bouns_record")->field("record.*,shop.nickname as shop")->alias("record")->order('id', 'desc');
        $param = $this->request->get();
        if (isset($param['order_id']) && $param['order_id']) {
            $obj->where('record.order_id', "like","%".$param['order_id']."%");
        }
        if (isset($param['card']) && $param['card']) {
            $obj->where('record.card',"like","%".$param['card']."%");
        }
        if (isset($param['shop_id']) && $param['shop_id']) {
            $obj->where('record.shop_id',$param['shop_id']);
        }

        if (isset($param['start']) && $param['start']) {
            $obj->where('record.time', '>', strtotime($param['start'] . " 00:00:00"));
        }
        if (isset($param['end']) && $param['end']) {
            $obj->where('record.time', '<', strtotime($param['end'] . " 00:00:00"));
        }
        $obj->join('tb_card_shop shop',"record.shop_id=shop.id","left");
        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);

        $shops =  Db::table("tb_card_shop")->select();
        return view('', ['list' => $list,'shops'=>$shops]);
    }


    public function list(){
        $obj = Db::table("tb_order")->order('id', 'desc');
        $param = $this->request->get();
        if (isset($param['word']) && $param['word']) {
            $obj->where('order_id|name', "like","%".$param['word']."%");
        }
        if (isset($param['status']) && $param['status']) {
            $obj->where('status',$param['status']);
        }
        if (isset($param['pay_status']) && $param['pay_status']) {
            $obj->where('pay_status',$param['pay_status']);
        }
        if (isset($param['start']) && $param['start']) {
            $obj->where('create_time', '>', strtotime($param['start'] . " 00:00:00"));
        }
        if (isset($param['end']) && $param['end']) {
            $obj->where('create_time', '<', strtotime($param['end'] . " 00:00:00"));
        }
        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }


    public function goods(){
        $obj = Db::table("tb_card_good")->field("id,good_id,plan,money,img")->order('id', 'desc');
        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    public function goodEdit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_card_good")->where("id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $sqldata = array();
            $sqldata["good_id"]=$data["good_id"];
            $sqldata["plan"]=$data["plan"];
            $sqldata["money"]=$data["money"];
            $sqldata["img"]=$data["img"];
            $sqldata["time"]=time();
            if (empty($id)){
                $res = Db::table("tb_card_good")->insert($sqldata);
            }else{
                $res = Db::table("tb_card_good")->where("id",$id)->update($sqldata);
            }
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败'];
            }
        }
        return view('', ['row' => $row]);
    }

    public function goodDel(){
        if ($this->request->isPost()) {
            $id = $this->request->param('id');
            if (empty($id)){
                return ['status' => 'n', 'info' => '参数错误'];
            }else{
                $res = Db::table("tb_card_good")->delete(["id"=>$id]);
            }
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败'];
            }
        }else{
            return ['status' => 'n', 'info' => '提交方式不正确'];
        }
    }


}